package com.automobile.shiro;

import com.automobile.realm.UserRealm;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy;
import org.apache.shiro.authc.pam.ModularRealmAuthenticator;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        Map<String, String> map = new HashMap<>();


        map.put("/user/logout.do", "logout");
        map.put("/feichechuli.html","authc");
        map.put("/upload.html","authc");
        map.put("/shopCart.html","authc");

        map.put("/**", "anon");

        shiroFilter.setFilterChainDefinitionMap(map);

//        shiroFilter.setSuccessUrl("/admin/indexList.html");
        shiroFilter.setLoginUrl("/login.html");
//        shiroFilter.setUnauthorizedUrl("/403.html");

        return shiroFilter;
    }


    @Bean
    public SecurityManager securityManager() {
        DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager();
//        securityManager.setSessionManager(sessionManager());
        securityManager.setAuthenticator(authenticator());
        securityManager.setRealm(userRealm());
        return securityManager;
    }

    @Bean
    public ModularRealmAuthenticator authenticator() {
        ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
        //如果有多个认证，需要每个认证都通过，默认只要通过一个认证即可
        authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
        return authenticator;
    }

    /**
     * 3. 配置 Realm
     * 3.1 直接配置实现了 org.apache.shiro.realm.Realm 接口的 bean
     * 将自己的验证方式加入容器
     *
     * @return
     */
    @Bean
    public UserRealm userRealm() {
        UserRealm userRealm = new UserRealm();
        HashedCredentialsMatcher hashedCM = new HashedCredentialsMatcher();
        hashedCM.setHashAlgorithmName("MD5");
        hashedCM.setHashIterations(1024);
        userRealm.setCredentialsMatcher(hashedCM);
        return userRealm;
    }

    @Bean
    public SessionManager sessionManager(){
        return new MySessionManager();
    }



    /**
     * 开启shiro aop注解支持.
     * 使用代理方式;所以需要开启代码支持;
     *
     * @param securityManager
     * @return
     */
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }


}
